﻿using System;
using System.Windows;
using System.Windows.Controls;
using GameLogic.Controls;

namespace NumberGuessing
{
    public partial class NumberGuessingGame : UserControl, IGameFeatures
    {
        // IGameFeatures events
        public event EventHandler<ScoresChangedEventArgs> ScoresChanged;
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        public event EventHandler<TimeChangedEventArgs> TimeChanged;
        
        //public event EventHandler PauseTimerFired;
        //public event EventHandler ResumeTimerFired;
        
        // Game values
        Random random = new Random();
        int numberRange; // Range of random numbers (e.g. 100 -> 1 to 100)
        int guessNumber; // Current number, player has to guess

        public NumberGuessingGame()
        {
            InitializeComponent();
        }

        //
        // Level Selection
        //
        private void _level1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            numberRange = 10;
            this.StartGame(numberRange);
        }

        private void _level2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            numberRange = 100;
            this.StartGame(numberRange);
        }

        private void _level3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            numberRange = 1000;
            this.StartGame(numberRange);
        }

        // 
        // Game Logic
        //
        private void _enter_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int userGuess;

            if (int.TryParse(_input.Text, out userGuess))
            {
                if (userGuess > guessNumber)
                {
                    _status.Text = "Falsch! Die geratene Zahl ist zu hoch.";
                    this.OnScoresChanged(-1);
                }
                else if (userGuess < guessNumber)
                {
                    _status.Text = "Falsch! Die geratene Zahl ist zu niedrig.";
                    this.OnScoresChanged(-1);
                }
                else
                {
                    _status.Text = "Richtig! Das war die gesuchte Zahl!";
                    this.OnScoresChanged(numberRange);

                    // End game
                    OnGameOver();
                }
            }
        }

        private void StartGame(int maxValue)
        {
            //highscoreEntry = false;

            // Go to game screen
            VisualStateManager.GoToState(this, "Game", true);
            _input.Text = "";

            // Set number to be guessed
            guessNumber = random.Next(maxValue) + 1;

            // Update text
            _challenge.Text = String.Format("Rate eine Zahl zwischen 1 und {0}", numberRange);
            _status.Text = "Bitte gib eine Zahl ein";
        }

        //
        // IGameFeatures Members
        //
        public void Start()
        {
            // Go to level selection
            VisualStateManager.GoToState(this, "LevelSelection", false);
        }

        private void OnGameOver()
        {
            if (this.GameOver != null)
            {
                GameOverEventArgs args = new GameOverEventArgs();
                args.Message = "Super";
                this.GameOver(this, args);
            }
        }


        public void OnLivesChanged(int lives)
        {

        }


        public void OnScoresChanged(int scores)
        {
            if (ScoresChanged != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Scores = scores;
                ScoresChanged(this, args);
            }
        }

        //public void ChangeLives(int p)
        //{

        //}

        public void Stop()
        {
        }

        public void Pause() 
        { 
        }

        public void Resume() 
        { 
        }

        public bool IsTimeBased
        {
            get { return false; }
        }

        public bool IsLifeBased
        {
            get { return false; }
        }

        public string GameName
        {
            get { return "Number Guessing"; }
        }

        private void _gameOverButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OnGameOver();
        }

        public TimeSpan GameDuration
        {
            get { throw new NotImplementedException(); }
        }

        public event EventHandler<GameOverEventArgs> GameOver;

        public string DeveloperName
        {
            get { return "Oliver Scheer"; }
        }

        public string CompanyName
        {
            get { return "Microsoft Deutschland GmbH"; }
        }

        public string Description
        {
            get { return string.Empty; }
        }

        public Uri SettingsPage
        {
            get { return null; }
        }



    }
}
